package com.kuaiyin.controller.user;


import com.kuaiyin.dto.DepartmentsDTO;
import com.kuaiyin.entity.Departments;
import com.kuaiyin.result.Result;
import com.kuaiyin.service.IDepartmentsService;
import com.kuaiyin.utils.PageResult;
import com.kuaiyin.vo.DepartmentsVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 部门信息表（含完整审计字段） 前端控制器
 * </p>
 *
 * @author ng
 * @since 2025-04-22
 */
@Tag(name = "部门管理")
@RestController
@RequestMapping("/departments")
public class DepartmentsController {

    @Autowired
    private IDepartmentsService departmentsService;

    @Operation(summary = "新增部门")
    @CacheEvict(cacheNames = "departments", allEntries = true)
    @PostMapping
    public Result<String> saveDepartments(@RequestBody @Validated DepartmentsDTO departmentsDTO) {
        departmentsService.saveDepartments(departmentsDTO);
        return Result.success("创建成功");
    }

    @Operation(summary = "删除部门")
    @CacheEvict(cacheNames = "departments", allEntries = true)
    @DeleteMapping("/{id}")
    public Result<String> deleteDepartments(@PathVariable Integer id) {
        departmentsService.removeById(id);
        return Result.success("删除成功");
    }

    @Operation(summary = "更新部门状态")
    @CacheEvict(cacheNames = "departments", allEntries = true)
    @PatchMapping("/{id}/status")
    public Result<String> updateStatus(@PathVariable Integer id,
                                       @RequestParam Boolean status) {
        Boolean updatedStatus = departmentsService.updateStatus(id, status);
        if (status == null) {
            return Result.error("状态不能为空");
        }
        return Result.success(updatedStatus ? "更新成功" : "更新失败");
    }

    @Operation(summary = "修改部门")
    @CacheEvict(cacheNames = "departments", allEntries = true)
    @PutMapping("/{id}")
    public Result<String> updateDepartments(@PathVariable Integer id, @RequestBody DepartmentsDTO departmentsDTO) {
        departmentsService.updatDepartments(id, departmentsDTO);
        return Result.success("更新成功");
    }

    @Operation(summary = "查询部门详情")
    @Cacheable(cacheNames = "departments", key = "#id")
    @GetMapping("/{id}")
    public Result<DepartmentsVO> getDepartments(@PathVariable Integer id) {
        Departments departmentsVO = departmentsService.getById(id);
        if (departmentsVO == null) {
            return Result.error("部门不存在");
        }
        DepartmentsVO vo = new DepartmentsVO();
        BeanUtils.copyProperties(departmentsVO, vo);
        return Result.success(vo);
    }


    @Operation(summary = "查询部门列表")
    @Cacheable(cacheNames = "departments", key = "'all'")
    @GetMapping("/all")
    public Result<List<Departments>> getDepartmentsList() {
        List<Departments> list = departmentsService.list();
        return Result.success(list);
    }

    @Operation(summary = "分页查询部门列表")
    @GetMapping("/search")
    public Result<PageResult<DepartmentsVO>> getDepartments(
            @RequestParam(required = false) String name,
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer size) {
        PageResult<DepartmentsVO> list = departmentsService.getDepartments(page, size, wrapper -> {
            if (StringUtils.hasText(name)) {
                wrapper.like(Departments::getName, name);
            }
            wrapper.orderByDesc(Departments::getId);
        });
        return Result.success(list);
    }
}
